package com.example.springboot3005web.controller;


import com.example.springboot3005web.vo.Person;
import com.example.springboot3005web.vo.User;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;

@RestController
public class ParameterController {

    // 一一对应，适合接收简单类型数据 String int Long double float，参数较少，比如 2个3个
    @RequestMapping("/param/p1")
    public String method1(String name, Integer age, String sex){
        return "接收参数：+" + name + age + sex;
    }


    // 使用对象接收参数，要求对象的属性名称和请求中参数名一样，属性有set方法，类有午餐构造方法
    @GetMapping("/param/p2")
    public String method2(Person person, String type) {
        return person.toString()+type;
    }

    // 使用HttpServletRequest接收参数
    @RequestMapping("/param/p3")
    public String method3(HttpServletRequest request) {
        String name = request.getParameter("name");
        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        return "name = " + name + ",age = " + age + ",sex = " + sex;
    }

    // RequestParam使用请求参数注解接收参数，这里只能使用 简单类型，不能接收对象
    @GetMapping("/param/p4")
    public String method4(@RequestParam(value = "name", required = true) String name,
                          @RequestParam(value = "age", required = false) Integer age,
                          @RequestParam(value = "sex", required = false, defaultValue = "男") String sex) {

        return "name = " + name + ",age = " + age + ",sex = " + sex;
    }
    // 使用RequestHeader接收请求头
    @GetMapping("/param/p5")
    public String method5(@RequestParam(value = "name", required = true) String name,
                          @RequestParam(value = "age", required = false) Integer age,
                          @RequestParam(value = "sex", required = false, defaultValue = "男") String sex,
                          @RequestHeader(value = "Accept") String accept) {

        return "name = " + name + ",age = " + age + ",sex = " + sex + ",accept = " + accept;
    }

    // @RequestBody接收JSON数据，post请求体数据

    /**
     * 前端参数
     * @RequestBody:从求求提中读取JSON数据，将数据转为形参对象的属性值
     *      框架会自动创建User对象，并将 username，age中的值赋给两个同名的属性
     *  注意：前端请求中需要指定：投Content-type:application/json
     */
    @PostMapping("/param/p6")
    public String method6(@RequestBody User user) {
        return user.toString();
    }

    // 使用 Reader/InputStream读取请求体post中的数据
    @PostMapping("/param/p7")
    public String method7(Reader reader) {

        StringBuilder buffer = new StringBuilder();
        BufferedReader bf = new BufferedReader(reader);
        String line="";
        try {
            while ((line = bf.readLine()) != null) {
                buffer.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return buffer.toString();
    }

    // 传入同名参数，也就是数组的值
    // 这里主要用于做删除操作时可能有用，一次性删除多个数据
    @GetMapping("/param/p8")
    public String method8(Integer[] id){
        List<Integer> ids = Arrays.stream(id).toList();
        return  "ids = " + ids;
    }

}
